<%@ page language="java" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 
<%@ taglib uri="/WEB-INF/webflow.tld" prefix="webflow" %> 
<%@ taglib uri="/WEB-INF/webform.tld" prefix="webform" %> 

<%@ page import="com.broadvision.bpmapp.docreview.Document" %>
<%@ page import="org.apache.commons.beanutils.PropertyUtils" %>

<html>
<head>
	<title><bean:message bundle="appCommonRes" key="dr.upload.title"/></title>
</head>

<body>
	
<% String uploadApp = application.getInitParameter("UPLOAD_CONTEXT"); %>
<script language="javascript">

// Add a function called trim as a method of the prototype
// object of the String constructor.
String.prototype.trim = function() {
  // Use a regular expression to replace leading and trailing
  // spaces with the empty string
  return this.replace(/(^\s*)|(\s*$)/g, "");
}

function isEmptyString(str){
   if (str == "" || str == null) return true;
   if (str.trim() == "") return true;
   return false;
}

function onClickDeleteDocument(index) {
  if (confirm("<bean:message bundle="appCommonRes" key="dr.confirm.deleteDocument"/>")) {
    document.uploadForm.formAction.value = "remove";
    document.uploadForm.removeIndex.value = index;
    document.uploadForm.encoding = "application/x-www-form-urlencoded";
    document.uploadForm.submit();
  }
}

function prepareSubmit(val) {
    if ( document.uploadForm.procName.value == "" ) {
      alert('<bean:message bundle="appCommonRes" key="dr.error.processName"/>');
      return;
    }
    if ( document.uploadForm.procDescription.value.length > 255 ) {
      alert('<bean:message bundle="appCommonRes" key="dr.error.processDescription"/>');
      return;
    }
    if (document.uploadForm.docCount.value == 0)  {
      alert('<bean:message bundle="appCommonRes" key="dr.error.cannotStartWorkflow"/>');
      return;
    }
    document.uploadForm.formAction.value = val;
    document.uploadForm.encoding = "application/x-www-form-urlencoded";
    document.uploadForm.submit();
}

function onClickAdd(theForm) {
    if ( isEmptyString(theForm.docName.value) ){
      alert('<bean:message bundle="appCommonRes" key="dr.error.docName"/>');
      return;
    }
    if ( theForm.docFile.value == "" ){
      alert('<bean:message bundle="appCommonRes" key="dr.error.docFile"/>');
      return;
    }
    theForm.formAction.value = "add";
    theForm.nextPage.value = theForm.action;
    theForm.action = '/<%= uploadApp %>'+'/upload.jsp';
    theForm.method = "post";
    theForm.submit();
    return false;
}

</script>

<jsp:useBean id="uploadView" scope="request" type="com.broadvision.web.util.ViewDynaBean"/> 

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="100%">
<div class="app_pageTitle"><bean:message bundle="appCommonRes" key="dr.upload.title"/></div> 

<webflow:encode url="/upload.do" id="submitURL"/>
<%String url = (String)pageContext.getAttribute("submitURL");%>
<html:form action="<%=url%>" enctype="multipart/form-data">

<html:hidden name="uploadView" property="GUID" />
<input type="hidden" name="formAction" value="add" />
<input type="hidden" name="removeIndex" value="0" />
<input type="hidden" name="store" value="fstore" />
<webform:hidden url="<%=url%>" />

<%-- added the following to compliant with jsp --%>
<input type="hidden" name="nextPage">

<div class="app_pageSubtitle">
  <span class="left"><bean:message bundle="appCommonRes" key="dr.process"/></span>
  <span class="right">&nbsp;</span>
</div>

<div class="blockWrap">
<table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0" summary="<bean:message bundle="appCommonRes" key="dr.processInfo"/>">
<tr> 
  <td class="label" width="10%"><bean:message bundle="appCommonRes" key="dr.name"/>*:</td>
  <td class="labelField" width="90%"><html:text name="uploadView" property="procName" size="40" maxlength="80" /></td>
</tr>

<tr> 
  <td class="label" width="10%"><bean:message bundle="appCommonRes" key="dr.description"/>:</td>
  <td class="labelField" width="90%"><html:textarea name="uploadView" property="procDescription" cols="40" rows="5" /></td>
</tr>			  

<tr> 
  <td class="label" width="10%"><bean:message bundle="appCommonRes" key="dr.type"/>:</td>
  <td class="labelField" width="90%"><html:text name="uploadView" property="type" size="40" maxlength="80" /></td>
</tr>

<tr> 
  <td class="label" width="10%"><bean:message bundle="appCommonRes" key="dr.subtype"/>:</td>
  <td class="labelField" width="90%"><html:text name="uploadView" property="subtype" size="40" maxlength="80" /></td>
</tr>
</table>
</div>

<div class="app_pageSubtitle">
  <span class="left"><bean:message bundle="appCommonRes" key="dr.document"/></span>
  <span class="right">&nbsp;</span>
</div>

<div class="blockWrap">
<table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0" summary="dr.documentInfo">
<tr> 
  <td class="label" width="10%"><bean:message bundle="appCommonRes" key="dr.name"/>*:</td>
  <td class="labelField" width="90%"><html:text property="docName" size="40" maxlength="80" /></td>
</tr>
			  
<tr> 
  <td class="label" width="10%"><bean:message bundle="appCommonRes" key="dr.description"/>:</td>
  <td class="labelField" width="90%"><html:textarea property="docDesc" cols="40" rows="4"/></td>
</tr>
			 
<tr> 
  <td class="label" width="10%"><bean:message bundle="appCommonRes" key="dr.file"/>*:</td>
  <td class="labelField" width="90%"><input type="file" name="docFile" size="40">
    <input hspace="40" type="button" name="addButton" class="inputButton" value="<bean:message bundle="appCommonRes" key="dr.add"/>" 
    	onClick="return onClickAdd(this.form)">
  </td>
</tr>
</table>  
		
<table class="dataTable" border="0" cellspacing="1" cellpadding="2" width="100%" summary="dr.uploadedDocList">
<tr class="listHeader"> 
  <th id="Name" align="left"><bean:message bundle="appCommonRes" key="dr.name"/></th>
  <th id="Description" align="left"><bean:message bundle="appCommonRes" key="dr.description"/></th>
  <th id="Filename" align="left" nowrap><bean:message bundle="appCommonRes" key="dr.filename"/></th>
  <th id="Actions" align="left" nowrap><bean:message bundle="appCommonRes" key="dr.actions"/></th>
</tr>     

<bean:size id="documentCount" name="uploadView" property="documentList" />
<input type="hidden" name="docCount" value='<%= pageContext.getAttribute("documentCount") %>'>
<logic:equal name="documentCount" value="0">
<tr class="listEven">
<td colspan="4" align="center"><bean:message bundle="appCommonRes" key="dr.noDocument"/></td>
</tr>
</logic:equal>

<logic:iterate id="doc" name="uploadView" property="documentList" indexId="index">  
<tr class='<%=index.intValue() %2 == 0 ? "listEven" : "listOdd"%>'>
  <td headers="Name"><bean:write name="doc" property="docName"/></td>
  <td headers="Description"><bean:write name="doc" property="docDesc"/></td>
  <td headers="Filename"> 
  <%
    // strip off the path name and just display the file name
    String str = (String)PropertyUtils.getSimpleProperty(doc, "filePath");
    int idx = str.lastIndexOf("/");
    if (idx == -1) {
  %>
  <%= str %>
  <% } else { %>
  <%= str.substring(idx+1, str.length()) %>
  <% } %>
  </td>
  <td headers="Action"><a href="javascript:onClickDeleteDocument(<bean:write name="index"/>)">
	<img src="<%= request.getContextPath() + "/cs/images/action/icon_delete.gif" %>" class="actionIcon" border="0" alt="<bean:message bundle="appCommonRes" key="dr.delete"/>" title="<bean:message bundle="appCommonRes" key="dr.delete"/>"></a></td>

</tr>
</logic:iterate>
</table>

<div class="pageButtonWrap" align="right">
<input hspace="40" type="button" name="doneButton" class="pageButton" value="<bean:message bundle="appCommonRes" key="dr.startWorkflow"/>" 
	onClick="javascript:prepareSubmit('done')">
</div>

</div>
</html:form>
</td>
</tr>
</table>
      
</body>
</html>
